package com.buer.storage.controller;

import com.buer.storage.commom.JsonResult;
import com.buer.storage.dto.*;
import com.buer.storage.entity.*;
import com.buer.storage.service.AorderService;
import com.buer.storage.utils.CheckEmptyUtil;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Aorder")

public class AorderController {
    @Resource
    private AorderService aorderService;

    //增加入库信息

    @PostMapping("/insertAorderStorage.do")
    public R insert(@RequestBody AorderStorageDto aorderStorageDto){
        System.out.println(aorderStorageDto);
        if (aorderService.insert(aorderStorageDto) > 0) {

            return R.setR(1, "调拨单保存成功", null);
        }

        return R.setR(0,"调拨单保存失败",null);
    }

    //根据订单ID查询入库信息
    @GetMapping("/findAorderAllById.do")
    public List<AorderSelectDto> findPutAll(Integer allotId){
        List<AorderSelectDto> aorderAllById = aorderService.findAorderAllById(allotId);
        return aorderAllById;
    }

    //查询所有订单信息
    @GetMapping("/findAorder.do")
    public List<AorderDto> findAorder(){
        List<AorderDto> aorderAll = aorderService.findAorder();
        return aorderAll;
    }

    @PostMapping("/selectByAll.do")
    public JsonResult selectByAll(@RequestBody Map<String,Object> params){

        PageInfo<AorderDto> entityList = aorderService.selectByAll(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
}

    //根据入库订单ID删除入库信息
    @GetMapping("/deleteAorderById.do")
    public R deleteAorderById(Integer allotId) {
        if (aorderService.deleteAorderById(allotId) > 0) {
            return R.setR(1, "删除调拨单成功", null);
        }
        return R.setR(0, "删除调拨单失败", null);
    }

    //更新ID删除商品信息
    @PostMapping("/update.do")
    public R update(@RequestBody AorderStorageDto aorderStorageDto) {
        if (aorderService.updateAorderdetails(aorderStorageDto) > 0) {
            return R.setR(1, "更新调拨单成功", null);
        }
        return R.setR(0, "更新调拨单失败", null);
    }

    //提交商品详情进行累加
    @GetMapping("/submitAddBuerStock.do")
    public R submitAddBuerStock(int allotId) {
        if (aorderService.submitAddBuerStock(allotId) > 0) {
            return R.setR(1, "调拨成功", null);
        }
        return R.setR(0, "调拨失败", null);
    }

    @PostMapping("/getAord")
    public JsonResult getAord(@RequestBody Map<String,Object> params) {
        PageInfo<AorderDto> entityList = aorderService.getAord(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }

    @PostMapping("/getAorderdetails")
    public JsonResult getAorderdetails(@RequestBody Map<String,Object> params) {
        PageInfo<AorderdetailsDto> entityList = aorderService.getAorderdetails(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }

    @PostMapping("/updateAorderdetails.do")
    public R updateAorderdetails(@RequestBody AllotBatchDto allotBatchDto) {
        aorderService.updateOrder(allotBatchDto);
        List<BatchAllotDto> list = allotBatchDto.getList();
        if (CheckEmptyUtil.isNotEmpty(list)) {
            for (BatchAllotDto batchAllotDto : list) {
                if (batchAllotDto.getType().equals("add")) {
                    aorderService.insertBatch(batchAllotDto);
                } else if (batchAllotDto.getType().equals("edit")) {
                    aorderService.updateBatch(batchAllotDto);

                } else if (batchAllotDto.getType().equals("delete")) {
                    aorderService.deleteBatch(batchAllotDto);
                }
            }
        }
        return R.setR(1, "修改成功", null);
    }
}
